package com.zy.spring02;

import com.zy.domain.Connection;
import com.zy.domain.Person;
import com.zy.domain.PersonFactory;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest2
{
    @Test
    //通过set方法进行依赖注入
    public void test1()
    {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-injection.xml");
        Person person1=(Person)applicationContext.getBean("personId");
        System.out.println(person1);
    }

    @Test
    //通过构造方法进行依赖注入
    public void test2()
    {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-injection.xml");
        Person person2=(Person)applicationContext.getBean("personId");
        System.out.println(person2);
    }

    @Test
    public void test3()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-connection.xml");
        Connection connection=applicationContext.getBean("co",Connection.class);
        System.out.println(connection);
    }

    //使用工厂的静态方法
    @Test
    public void test4()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-create.xml");
        Person person=applicationContext.getBean("p2", Person.class);
        System.out.println(person);
    }
    //使用工厂的非静态方法
    @Test
    public void test5()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-create.xml");
        Person person=applicationContext.getBean("p3", Person.class);
        System.out.println(person);
    }
}
